package com.sunshine.lnuplus.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.viewpager.widget.PagerAdapter
import com.sunshine.lnuplus.R
import com.sunshine.lnuplus.model.InitData
import com.sunshine.lnuplus.utils.DateUtils
import com.sunshine.lnuplus.view.LessonLayout

class ChooseWeekRecyclerAdapter(
    private val fm: FragmentManager,
    private val fragment: Fragment
) : PagerAdapter() {

    override fun getCount(): Int {
        return InitData.lessonStyle.weekCount
    }

    override fun isViewFromObject(view: View, `object`: Any): Boolean {
        return view === `object`
    }

    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        val linearLayout = LayoutInflater.from(container.context).inflate(
            R.layout.item_choose_week,
            null
        ) as LinearLayout
        linearLayout.id = R.id.item_id
        linearLayout.findViewById<LessonLayout>(R.id.choose_week_timetable).init(
            lessonList = InitData.lessonList!!,
            lessonStyle = InitData.lessonStyle,
            thisWeek = position + 1,
            otherWeek = DateUtils.getThisWeek(InitData.lessonStyle.startMonth, InitData.lessonStyle.startDay),
            fragmentManager = fm,
            isSetting = false,
            fragment = fragment,
            adapter = this)
        container.addView(linearLayout)
        return linearLayout
    }

    override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
        val view = `object` as LinearLayout
        container.removeView(view)
    }

    override fun getItemPosition(`object`: Any): Int {
        return POSITION_NONE
    }
}